//
//  TaskItemDetailView.swift
//  VenusTodo
//
//  Created by 文显庆 on 2021/10/9.
//

//修改任务视图

import SwiftUI

struct TaskItemDetailView: View {
    @State var preView: TodoView
    @State var task: ThingsTodo
    @State var isdone: Bool = false
    @State var target: String = "" //任务目标
    @State var progress: String = "" //当前进度
    let items = ["是", "否"]
    @State var done: String = ""//是否完成
    
    //错误提示信息
    @State private var errorShowing = false
    @State private var errorTitle = ""
    @State private var errorMessage = ""
    @State private var submit = false
    @State var taskName : String = ""
    

    var body: some View {
        List {
            Section(header: Text("任务名称")){
                HStack {
                    TextField("请输入任务名称", text: $taskName, onEditingChanged: { (value) in print("onEditingChanged:\(self.taskName)")}
                    )
                }
            }
            if task.taskIndex == 0 {
                Section(header: Text("任务目标")){
                    HStack {
                        TextField("请输入目标", text: $target, onEditingChanged: { (value) in print("onEditingChanged:\(self.target)")}
                        )
                    }
                }
                Section(header: Text("当前进度")){
                    HStack {
                        TextField("请输入当前进度", text: $progress, onEditingChanged: { (value) in print("onEditingChanged:\(self.progress)")}
                        )
                    }
                }
            } else {
                Section(header: Text("完成情况")){
                    HStack {
                        Picker("完成情况", selection: $done) {
                            ForEach(items, id: \.self) {
                                Text($0)
                            }
                        }
                    }
                }
            }
            
            
            Button(action: EditItem, label:{ Text("提交修改")
                    .font(.system(size: 24, weight: .bold, design: .default))
                    .padding()
                    .frame(minWidth: 0, maxWidth: .infinity)
                    .background(Color.blue)
                    .cornerRadius(9)
                    .foregroundColor(.white)
            } )
           
        }.alert(isPresented: $errorShowing, content: {
            Alert(title: Text(errorTitle),  message: Text(errorMessage), dismissButton: .default(Text("确认")))
        })
        .alert(isPresented: $submit, content: {
            Alert(title: Text("提示"),  message: Text("信息修改成功"), dismissButton: .default(Text("确认")))
        })
    
        
    }
    
    
   
    func EditItem() {
        if task.taskIndex == 0 {
            if(self.taskName == ""){
                self.errorShowing = true
                self.errorTitle = "无效的名称"
                self.errorMessage = "请检查您的任务名称"
                return
            }else if(self.target == ""){
                self.errorShowing = true
                self.errorTitle = "无效的目标"
                self.errorMessage = "请检查您的任务目标"
                return
            } else{
                for i in 0..<self.preView.Tasks.count {
                    if(self.preView.Tasks[i].id == task.id) {
                        self.preView.Tasks[i].name = taskName
                        self.preView.Tasks[i].progress = Int(self.progress)!
                        self.preView.Tasks[i].target = Int(self.target)!
                        if self.progress >= target {
                            self.preView.Tasks[i].isDone = true
                        } else {
                            self.preView.Tasks[i].isDone = false
                        }
                        break
                    }
                }
                save()
                self.submit = true
            }
        } else {
            for i in 0..<self.preView.Tasks.count {
                if(self.preView.Tasks[i].id == task.id) {
                    self.preView.Tasks[i].name = taskName
                    if self.done == "是" {
                        self.preView.Tasks[i].isDone = true
                    } else  {
                        self.preView.Tasks[i].isDone = false
                    break
                }
            }
            save()
            self.submit = true
        }
        
        
    }
    //本地存储数据
    func save() {
        do{
            let data = try JSONEncoder().encode(self.preView.Tasks)
            UserDefaults.standard.setValue(data, forKey: "Tasks")
        }catch {
            print("Error")
        }
    }
}
}
